<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学号" prop="studentNo">
      <el-input v-model="dataForm.studentNo" disabled placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="学员姓名" prop="studentName">
      <el-input v-model="dataForm.studentName" placeholder="学员姓名"></el-input>
    </el-form-item>
    <!-- <el-form-item label="学员状态" prop="status">
      <el-select v-model="dataForm.status" @change="bindStudentChange" placeholder="请选择">
        <el-option
          v-for="item in studentStatusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="性别">
      <el-select v-model="dataForm.gender" placeholder="请选择">
        <el-option
          v-for="item in genderOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-date-picker
      v-model="dataForm.birthday"
      type="date"
      placeholder="选择出生日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="校区" prop="schoolId">
      <el-select v-model="dataForm.schoolId"  placeholder="请选择">
        <el-option
          v-for="item in schoolNameOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="年级" prop="gradeId">
      <el-select v-model="dataForm.gradeId"  placeholder="请选择">
        <el-option
          v-for="item in gradeNameOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="就读学校" prop="realSchoolName">
      <el-input v-model="dataForm.realSchoolName" placeholder="就读学校"></el-input>
    </el-form-item>
    <!-- <el-form-item label="班级" prop="classId">
      <el-select v-model="dataForm.classId"  placeholder="请选择">
        <el-option
          v-for="item in classIdOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="账号" prop="mobile">
      <el-input v-model="dataForm.mobile" disabled placeholder="小程序登录账号"></el-input>
    </el-form-item>
    <!-- <el-form-item label="是否老学员" prop="isOld">
      <el-select v-model="dataForm.isOld"  placeholder="请选择">
        <el-option
          v-for="item in isOldOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="入班考试分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="入班考试分数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getNowFormatDate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        gender: 2,
        studentStatusOptions: [{
            value: 0,
            label: '正常'
          }, {
            value: 1,
            label: '离班'
          }
        ],
        genderOptions:  [{
            value: 1,
            label: '女'
          }, {
            value: 2,
            label: '男'
          }
        ],
        schoolNameOptions:   [],
        gradeNameOptions:  [],
        isOldOptions: [{
            value: 1,
            label: '是'
          }, {
            value: 2,
            label: '否'
          }
        ], 
        classIdOptions:  [{
            value: '1',
            label: '第一班级'
          }, {
            value: '2',
            label: '第二班级'
          }
        ],
        

        dataForm: {
          id: 0,
          studentNo: '',
          studentName: '',
          status: '',
          gender: '',
          birthday: '',
          schoolId: '',
          gradeId: '',
          realSchoolName: '',
          classId: '',
          unionId: '',
          isOld: '',
          isValid: '',
          score: '',
          isDeleted: '',
          gmtCreate: '',
          gmtModified: '',
          mobile: '',
        },
        dataRule: {
          studentNo: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          studentName: [
            { required: true, message: '学员姓名不能为空', trigger: 'blur' }
          ],
          // status: [
          //   { required: true, message: '学员状态（0 正常 1 离班）不能为空', trigger: 'blur' }
          // ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          // birthday: [
          //   { required: true, message: '生日不能为空', trigger: 'blur' }
          // ],
          schoolId: [
            { required: true, message: '校区不能为空', trigger: 'blur' }
          ],
          gradeId: [
            { required: true, message: '年级不能为空', trigger: 'blur' }
          ],
          // classId: [
          //   { required: true, message: '班级不能为空', trigger: 'blur' }
          // ],
          // isOld: [
          //   { required: true, message: '是否老学员不能为空', trigger: 'blur' }
          // ],
          // isValid: [
          //   { required: true, message: '是否通过校验不能为空', trigger: 'blur' }
          // ],
          score: [
            { required: true, message: '入班考试分数不能为空', trigger: 'blur' }
          ],
          // isDeleted: [
          //   { required: true, message: '逻辑删除 1（true）已删除， 0（false）未删除不能为空', trigger: 'blur' }
          // ],
          // gmtCreate: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // gmtModified: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      bindStudentChange(){

      },
      //获取公共列表
      getCommonList() {
          this.$http({
            url: this.$http.adornUrl('/sys/common/list'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if(data && data.code === 0) {
                let schoolOptions = data.school || [];
                let gradeOptions = data.grade || [];

                schoolOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })

                gradeOptions.forEach((item) => {
                    item.value = item.id;
                    item.label = item.name;
                })


                this.schoolNameOptions = schoolOptions;
                this.gradeNameOptions = gradeOptions;


            }
          })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/edustudent/info/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.studentNo = data.eduStudent.studentNo
                this.dataForm.studentName = data.eduStudent.studentName
                this.dataForm.status = data.eduStudent.status
                this.dataForm.gender = data.eduStudent.gender
                this.dataForm.birthday = data.eduStudent.birthday
                this.dataForm.schoolId = data.eduStudent.schoolId
                this.dataForm.gradeId = data.eduStudent.gradeId
                this.dataForm.realSchoolName = data.eduStudent.realSchoolName
                this.dataForm.classId = data.eduStudent.classId
                this.dataForm.unionId = data.eduStudent.unionId
                this.dataForm.isOld = data.eduStudent.isOld
                this.dataForm.isValid = data.eduStudent.isValid
                this.dataForm.score = data.eduStudent.score
                this.dataForm.isDeleted = data.eduStudent.isDeleted
                this.dataForm.gmtCreate = data.eduStudent.gmtCreate
                this.dataForm.gmtModified = data.eduStudent.gmtModified
                this.dataForm.mobile = data.eduStudent.mobile
                this.gmtCreate = data.eduStudent.gmtCreate
                this.dataForm.id = data.eduStudent.id
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/edustudent/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'studentNo': this.dataForm.studentNo,
                'studentName': this.dataForm.studentName,
                'status': this.dataForm.status,
                'gender': this.dataForm.gender,
                'birthday': this.dataForm.birthday,
                'schoolId': this.dataForm.schoolId,
                'gradeId': this.dataForm.gradeId,
                'realSchoolName': this.dataForm.realSchoolName,
                'classId': this.dataForm.classId,
                'unionId': this.dataForm.unionId,
                'isOld': this.dataForm.isOld,
                'isValid': this.dataForm.isValid,
                'score': Number(this.dataForm.score),
                'mobile': Number(this.dataForm.mobile),
                // 'isDeleted': this.dataForm.isDeleted,
                // 'gmtCreate':this.dataForm.id ? this.dataForm.gmtCreate : getNowFormatDate(),
                // 'gmtModified': getNowFormatDate()
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
